package hyo.hun.lifemanager.common;

import hyo.hun.lifemanager.common.DBSQLite.ParseSelectData;
import hyo.hun.lifemanager.today.TodayData;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

/**
 * 자주 사용되는 select문을 만들어놓음
 * @author HYOJUN
 *
 */
public class DBQuery 
{
	private DBSQLite dbSql;
	private Context context;
	
	public DBQuery(DBSQLite dbSql, Context context)
	{
		this.context = context;
		this.dbSql = dbSql;
		//dbSql = DBSQLite.getInstance(context);
	}
	
	private ArrayList<TodayData> listTodayData;

	/**
	 * 오늘 데이터 테이블의 전부를 가져온다
	 * @return TodayData
	 */
	public ArrayList<TodayData> getTodayTableData()
	{
		return getTodayTableData("");
	}
	/**
	 * 오늘 데이터 테이블의 where절 추가시
	 * @param addQuery
	 * @return TodayData
	 */
	public ArrayList<TodayData> getTodayTableData(String addQuery)
	{
		listTodayData = new ArrayList<TodayData>();
		
		String query = "SELECT * from today " + addQuery + ";";
		
		dbSql.selectMapTile(context, query, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				c.moveToFirst();
				
				if (count != 0)
				{
					// 변경해줘야할 부분
					int idIndex = c.getColumnIndex("id");
					int textIndex = c.getColumnIndex("text");
					
					for (int i = 0; i < count; i++)
					{
						TodayData data = new TodayData();
						
						data.setId(c.getString(idIndex));
						data.setText(c.getString(textIndex));
						
						listTodayData.add(data);
						
						c.moveToNext();
					}
					// 여기까지
				}
			}
		});
		
		return listTodayData;
	}
	
	
}
